<template>
    <div class="cmdcfg">
        <exInput v-model.number="x1" label="x" width="75" size="mini"></exInput>
        <exInput v-model.number="waitTime" label="等待(毫秒)" width="70" size="mini"></exInput>
        <br/>
        <exInput v-model.number="y1" label="y" width="75" size="mini"></exInput>
        <exInput v-model.number="keep" label="持续(秒)" width="70" size="mini"></exInput>
        <span v-if="isRun || true">进度: {{curIdx}} / {{totalCnt}}</span>
    </div>
</template>


<script>

export default {
    props: {
        cmdInfo: { type: Object, default: {} },
        devInfo:{ type: Object, default: {} },
    },
    data() {
        return {
            waitTime : this.cmdInfo.waitTime || 50,
            keep : this.cmdInfo.keep || 10,
            x1 : this.cmdInfo.x1 || "",
            y1 : this.cmdInfo.y1 || "",
            sTimer : null,
            curIdx : 0,
            isRun : false,
        };
    },
    inject: ['doDevCmd','doNextCmd'],
    computed:{
        totalCnt(){
            if(!this.waitTime) return 0;
            return this.keep * 1000 / this.waitTime;
        },
    },

    mounted() {
        this.$once('hook:beforeDestroy', () => {            
            clearInterval(this.sTimer);
        })
    },

    methods: {
        setByDevInfo(){
            this.x1 = this.devInfo.clickX || "";
            this.y1 = this.devInfo.clickY || "";
            if(this.cmdInfo.waitTime) this.waitTime = this.cmdInfo.waitTime;
            if(this.cmdInfo.keep) this.keep = this.cmdInfo.keep;
        },
        saveCmd(){
            this.cmdInfo.x1 = this.x1;
            this.cmdInfo.y1 = this.y1;
            this.cmdInfo.waitTime = this.waitTime;
            this.cmdInfo.keep = this.keep;
        },
        preCheck(){
            if(!this.x1){
                this.$message.error("请输入X坐标")
                return false;
            }
            if(!this.y1){
                this.$message.error("请输入Y坐标")
                return false;
            }
            if(!this.waitTime){
                this.$message.error("请输入等待时间")
                return false;
            }
            if(!this.keep){
                this.$message.error("请输入持续时间")
                return false;
            }
            return true;
        },
        runCmd(isOnce){
            if(!this.preCheck()){
                this.$emit("stopRun")
                return;
            }
            this.preView();
            if(this.curIdx!=0){
                this.stopCmd();
            }
            
            this.sTimer=setInterval(()=>{
                if(this.curIdx>this.totalCnt){
                    this.stopCmd();
                    this.$emit("stopRun")
                    if(isOnce!==true) this.doNextCmd();
                    return;
                }
                this.doDevCmd(`adb shell input tap ${this.x1*this.devInfo.devBet} ${this.y1*this.devInfo.devBet} `);
                this.curIdx++;
            },this.waitTime)
        },
        preView(){
            this.devInfo.clickX = this.x1;
            this.devInfo.clickY = this.y1;
        },
        stopCmd(){
            this.curIdx = 0;
            clearInterval(this.sTimer);
            this.$emit("stopRun")
        }
    }
}
</script>

<style lang="less" scoped>
    .cmdcfg{
        display: inline-block;
    }
</style>
